#include "chooselevelscene.h"
#include<mypushbutton.h>
#include<playscene.h>
#include<QSoundEffect>
#include<QMenuBar>
#include<QMenu>
#include<QPainter>
#include<QDebug>
#include<QLabel>

ChooseLevelScene::ChooseLevelScene(QWidget *parent)
    : QMainWindow{parent}
{
    //配置选择大小场景
    this->setFixedSize(420,688);

    //设置图标
    this->setWindowIcon(QPixmap(":/res/new20240507154444.png"));

    //设置标题
    this->setWindowTitle("The SelectLevelScene");

    //创建菜单栏
    QMenuBar*bar=menuBar();
    setMenuBar(bar);

    //创建开始菜单
    QMenu*startMenu= bar->addMenu("开始");

    //创建退出菜单项
    QAction*quitAction= startMenu->addAction("退出");

    //点击退出 实现退出游戏
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //选择关卡按钮的音效
    QSoundEffect*chooseSound=new QSoundEffect;
    chooseSound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));
    //返回关卡按钮的音效
    QSoundEffect*backSound=new QSoundEffect;
    backSound->setSource(QUrl::fromLocalFile(":/res/BackButtonSound.wav"));

    //返回按钮
    MyPushButton*backBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    //设立关于倒计时的定时器

    ti1=new QTimer(this);
    ti1->start(1000);


    //点击返回
    connect(backBtn,&MyPushButton::clicked,[=](){
        //播放返回按钮音效
        backSound->play();
        qDebug()<<"点击了返回の按钮";
        qDebug()<<"点击了返回按钮";
        //告诉主场景 我返回了，主场景监听ChooseLevelScene的返回按钮

        //播放返回按钮音效
        backSound->play();
        qDebug()<<"点击了返回の按钮";

        emit this->SelectSceneBack();
    });

    //创建选择关卡的按钮
    for(int i=0;i<20;i++)
    {
        MyPushButton*backgroundBtn=new MyPushButton(":/res/LevelIcon.png");
        backgroundBtn->setParent(this);
        //设置选择关卡按钮分布的位置
        backgroundBtn->move(80+i%4*70,160+i/4*70);

        //监听每个按钮的点击事件
        connect(backgroundBtn,&MyPushButton::clicked,[=](){
            //播放选择关卡按钮音效
            chooseSound->play();

            QString str=QString("您选择的是第%1关").arg(i+1);
            qDebug()<<str;



            //进入到游戏场景
            this->hide();//将选关场景隐藏掉
            play=new PlayScene(i+1);//创建游戏场景
            play->setGeometry(this->geometry());
            play->show();//显示游戏场景
            connect(ti1,&QTimer::timeout,play,&PlayScene::timeOutThing);//


            connect(play,&PlayScene::SelectSceneBack,[=](){
                this->setGeometry(play->geometry());
                this->show();
                delete play;
                play=NULL;
            });
        });

        QLabel*label=new QLabel;
        label->setParent(this);
        label->setFixedSize(backgroundBtn->width(),backgroundBtn->height());
        label->setText(QString::number(i+1));
        label->move(80+i%4*70,160+i/4*70);

        //设置label上的文字对齐方式 水平居中和 垂直居中
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        //设置让鼠标进行穿透 51号属性
        label->setAttribute(Qt::WA_TransparentForMouseEvents);

    }
}



void ChooseLevelScene::paintEvent(QPaintEvent*)
{
    //加载背景
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/20240507154901.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
    pix.load(":/res/20240507154444.png");
    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix.width(),pix.height(),pix);

}
